// pages/carInfo/carInfo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    formlist: [
      { isrequire: true, label: '品牌车系', key: 'brandCarName', valueType: 'text', tips: '请选择', type: 'input', url: '/pages/carInfodetail/carInfodetail'},
      { isrequire: true, label: '车牌号', key: 'plateNumber', valueType: 'text', tips: '请选择', type: 'input'},
      { isrequire: true, label: '车身颜色', key: 'carColor', valueType: 'text', tips: '请选择', type: 'select', data: ['黑色', '白色', '红色', '蓝色', '绿色', '黄色', '橙色', '青色', '紫色', '银色', '金色', '灰色'] },
      { isrequire: true, label: '车座', key: 'seat', valueType: 'number', tips: '请选择', type: 'input' },
      // { isrequire: true, label: '收款码', key: 'receiptCode', valueType: 'number', tips: '请上传', type: 'input', url:'/pages/receiptCode/receiptCode' },
    ],
    formData:{
      brandCar:"",
      plateNumber:'',
      carColor:'',
      seat:'',
      brandCarName:''
    },
    inputShow: false,
    key: 'name',
    valueType: 'text',
    currentInput: {},
    active: '',
    canSubmit:false,
    isHome:false,
  },
  bindPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    if (item.data) {
      this.data.formData[item.key] = item.data[e.detail.value]
    } else {
      this.data.formData[item.key] = e.detail.value
    }
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isHome) {
      this.setData({
        isHome: true
      })
    }
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    
  },
  changeTextarea: function (e) {//输入框值改变事件
    var key = e.target.dataset.value;
    this.data.formData[key] = e.detail.value;
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  saveCarInfo:function(e){
    if (this.data.canSubmit){
      this.data.formData.carDetail = '已填写';
      wx.setStorage({
        key: 'personalInfo',
        data: this.data.formData,
      })
      wx.navigateBack({
        delta: 1,
      })
      // if (this.data.isHome){
      //   wx.navigateTo({
      //     url: "/pages/perfectInfo/perfectInfo?flag=true&isHome"
      //   })
      // }else{
      //   wx.navigateTo({
      //     url: "/pages/perfectInfo/perfectInfo?flag=true"
      //   })
      // }
      
    }
  },
  inputTap:function(e){
    wx.setStorage({
      key: 'personalInfo',
      data: this.data.formData,
    })
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  changeValue() {
    var flag = true;
    var item = this.data.formData;
    if ((item.brandCar == '') || (item.plateNumber == '') || (item.carColor == '') || (item.seat == '')){
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'personalInfo',
      success: function (res) {
        // if (options.brandCar) {
        //   res.data.brandCar = options.brandCar;
        //   res.data.brandCarName = options.brandCarName;
        // }
        for (var j in that.data.formData) {
          if (res.data[j] != undefined) {
            that.data.formData[j] = res.data[j];
          } else {
            res.data[j] = that.data.formData[j];
          }
        }
        that.setData({
          formData: res.data
        })
        // if (options.imgUrl) {
        that.changeValue();
        // }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})